package com.jule.miaozhuapc.biz;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;

import com.jule.miaozhuapc.dao.UserDao;
import com.jule.miaozhuapc.domain.WawaPay;
import com.jule.miaozhuapc.domain.WawaRoom;
import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.util.Const;



public class UserCom {

	private Logger logger = Logger.getLogger(this.getClass());
	private EntityManager em;
	@PersistenceContext
	public void setEm(EntityManager em) {
		this.em = em;
	}
//	
//	/**
//	 *  今日是否签到
//	 * @param userId
//	 * @return
//	 * @throws Exception
//	 */
//	public Boolean todayCheck(Long userId) throws Exception{
//		String tsql = "select * from wawa_check_today where time =  DATE_FORMAT(now(), '%Y%m%d')  and userId = ? and isDelete = 0";
//		CheckToday today = db.findSimpleRefResult(tsql, CheckToday.class, userId);
//		return today != null;
//	}
//	
//	/**
//	 *  获取昨日签到数据
//	 * @param userId
//	 * @return
//	 * @throws Exception
//	 */
//	public CheckToday getYesterdayTodayCheck(Long userId) throws Exception{
//		// 获取昨天前天
//		String tsql = "select * from wawa_check_today where userId = ? and isDelete = 0 and time = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d') ";
//		CheckToday check  = db.findSimpleRefResult(tsql, CheckToday.class, userId);
//		return check;
//	}
//	/**
//	 *  获取配置信息
//	 * @param key
//	 * @return
//	 * @throws Exception
//	 */
//	public String getWaWaConfig(String key) throws Exception {
//		String sql = "select * from wawa_config where `key` = ? ";
//		Map<String , Object> map = db.findSimpleResult(sql, key);
//		if(map != null && map.size() > 0) {
//			return map.get("value")+"";
//		}
//		return "";
//	}
	

	/**
	 * 流水 表  软提交 外部触发 commit
	 * @param userId
	 * @param coin
	 * @param money
	 * @param type  流水类型
	 * @param remark
	 * @param keyId 外键ID
	 * @return
	 * @throws Exception
	 */
	public  int addCoin(Long userId, int coin, Double money, int type, String remark, Long keyId) throws Exception{
		
		// 充钱记录
		//注册
		WawaPay pay = new WawaPay();
		pay.setAddTime(new Date());
		pay.setRemark(remark);
		pay.setIsDelete(false);
		pay.setUserId(userId);
		pay.setType(type);
		pay.setCoin(coin);
		pay.setMoney(money);
		int len = 0;
		try {
			// 签到
			if ( type == Const.WAWA_PAY_TYPE_CHECK_TODAY) {
				pay.setCheckTodayId(keyId);
			}
			// 邀请
			else if ( type == Const.WAWA_PAY_TYPE_INVITE_USER) {
				pay.setInviteUserId(keyId);
			}
			// 消费 抓娃娃
			else if ( type == Const.WAWA_PAY_TYPE_GAME_RECORD) {
				pay.setGameRecordId(keyId);
			}
			em.persist(pay);
			// 加剩余金币
			UserDao userDao = (UserDao) em;
			WawaUser user = userDao.findByUserId(userId); //此时的person 已经脱离容器的管理
			user.setCoin(user.getCoin()+ coin);
			em.merge(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return len;
	}
	/**
	 * 邀请码Code
	 */

	public String getInviteCode(int len) throws Exception {
		char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
				'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		Random random = new Random();

		// 生成一个 len长度的 随机字符串
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < len; i++) {
			buff.append(chars[random.nextInt(chars.length)]);
		}

		logger.debug(buff);
		return buff.toString();
	}
	public static void main(String[] args)  throws Exception{
		// TODO Auto-generated method stub
		UserCom userCom = new UserCom();
		//userCom.setEm(em);
		userCom.addCoin(760L, 100, 0.0, 1, "springTest", null);
	}

}
